<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/4
 * Time: 10:03
 */

namespace Admin\Controller;


class AdController extends AdminController
{
    public function _myinitialize()
    {
        $this->db = M('ad');
        $this->db1 = M('ad_code_template');
        $this->username = session('user_auth.username');
    }

    //广告列表
    public function lst()
    {
        $where = array(
            'status' => 1,
        );
        //查出所有广告模板的数据
        $templates = $this->db1->where($where)->getField('id,template_name', true);
        //查出所有广告的数据
        $lists = $this->db->where($where)->order("update_time desc")->select();

        //在lists数据中加入template_name
        foreach ($lists as $k => $v) {
            $lists[$k]['template_name'] = $templates[$v['template_id']];
        }
        $this->assign('templates', $templates);
        $this->assign('lists', $lists);
        $this->display();
    }

    //广告新增
    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['update_user'] = $this->username;
                $data['update_time'] = time();
            }
            $rid = $this->db->add($data);
            if ($rid === false) {
                $this->error($this->db->getDbError());
            }
            $this->redirect('lst');
        } else {
            $where = array(
                'status' => 1,
            );
            //查出所有广告模板的数据
            $templates = $this->db1->where($where)->getField('id,template_name', true);
            $this->assign('templates', $templates);
            $this->assign('istype', "add");
            $this->display();
        }
    }

    //广告编辑
    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['update_user'] = $this->username;
                $data['update_time'] = time();
            }
            $rid = $this->db->save($data);
            if ($rid === false) {
                $this->error($this->db->getDbError());
            }
            $this->redirect('lst');
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();

            $where = array(
                'status' => 1,
            );
            //查出所有广告模板的数据
            $templates = $this->db1->where($where)->getField('id,template_name', true);

            $this->assign('templates', $templates);
            $this->assign('data', $data);
            $this->assign('istype', "edit");
            $this->display("add");
        }
    }

    //广告删除
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            //根据广告id到广告内容表中去查数据，如果当前删除的广告下有广告数据，则无法删除
            $count = M('ad_content')->where(array("ad_id" => $id, "status" => 1))->count("id");
            if ($count > 0) {
                $this->error("删除失败，本广告下还有" .$count. "条数据", $_SERVER["HTTP_REFERER"]);
            } else {
                $data = array(
                    'status' => 0,
                    'update_time' => time(),
                    'update_user' => $this->username,
                );
                $this->db->where(array('id' => $id))->save($data);
                $this->redirect('lst');
            }
        }
    }
}